/**
 * 
 */
package com.yidu.user.view;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.model.ManageUser;
import com.yidu.model.Page;
import com.yidu.user.service.ManageUserService;

@Controller
@RequestMapping("/manage")
public class ManageUserController {
	@Resource(name="userService")
	private ManageUserService userService;
	
	/**
	 * 进入登录界面
	 * @return
	 */
	@RequestMapping("/login")
	public String login(){
		return "login";
	}
	@RequestMapping("/loginSucces")
	public String loginSucces(ManageUser user,HttpServletRequest request,HttpSession session){
		boolean flag = userService.getCountByUserName(user);
		if("admin".equals(user.getUserName()) && "123".equals(user.getUserPwd())){
			session.setAttribute("admin", user.getUserName());
			return "redirect:/manage/admin";
		}else{
			if(flag){
				session.setAttribute("name", user.getUserName());
				return "redirect:/manage/index";
			}else{
				request.setAttribute("msg", "用户名或密码错误！");
				return "login";
			}
		}
	}
	@RequestMapping("/admin")
	public String adminIndex(){
		return "adminIndex";
	}
	/**
	 * 显示主页面
	 * @return
	 */
	@RequestMapping("/index")
	public String index(){
		return "index";
	}
	@RequestMapping("/regist")
	public String regist(){
		return "regist";
	}
	@RequestMapping("/registSucces")
	public String registSucces(ManageUser user){
		userService.insertSelective(user);
		return "login";
	}
	@RequestMapping("/selectName")
	public void selectName(String vuName,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
	}
	@RequestMapping("/list")
	public String showList(HttpServletRequest request,Page page){
		List<ManageUser> list = userService.getAllUser(page);
		request.setAttribute("list", list);
		request.setAttribute("userName",page.getName() );
		return "list";
	}
	@RequestMapping("/delete")
	public void delete(String userId,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/plain");
		int row = userService.deleteByPrimaryKey(userId);
		PrintWriter out = response.getWriter();
		System.out.println(row);
		if(row>0){
			out.print("ok");
		}
		out.close();
	}
	@ResponseBody
	@RequestMapping("/showUpdate")
	public ManageUser showUpdate(ManageUser user) {
		String id = user.getUserId();
		ManageUser us = userService.showUpdate(id);
		return us;
	}
	@RequestMapping("/addOrUpdate")
	public String addOrUpdate(ManageUser user){
		userService.updateByPrimaryKeySelective(user);
		return "redirect:list";
	}
	
}
